package com.fxl.majiang.message;

import com.alibaba.fastjson.JSON;
import com.fxl.majiang.message.output.OutputMessage;
import com.fxl.majiang.websocket.ClientManager;
import com.fxl.majiang.websocket.WebSocketServer;
import lombok.SneakyThrows;

import java.util.List;

/**
 * @author fengxinglei
 * @date 2022/8/7 11:27
 * @description 消息处理
 */
public class MessageHandler {

    /**
     * 给用户推送消息
     * @param userId
     * @param outputMessage
     */
    @SneakyThrows
    public static void sendToUser(String userId, OutputMessage outputMessage) {
        WebSocketServer server = ClientManager.get(userId);
        server.getSession().getBasicRemote().sendText(JSON.toJSONString(outputMessage));
    }

    /**
     * 批量这些用户推送消息
     * @param userIds
     * @param outputMessage
     */
    public static void sendToUsers(List<String> userIds, OutputMessage outputMessage) {
        for (String userId : userIds) {
            sendToUser(userId, outputMessage);
        }
    }
}
